package org.lyon_yan.JBCS_framework.listener;

import java.util.ArrayList;
import java.util.List;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.web.PopupFeatures;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.util.Callback;
import netscape.javascript.JSObject;

import org.lyon_yan.JBCS_framework.config.R;

/**
 * JBC_APP页面的JS监听
 * 
 * @author Lyon_Yan
 * @since 2015-08-06 09:51:57
 * @version 8.0.1
 *
 */
public class JavaScriptListerner {
	private JSObject jsObject = null;
	private WebEngine webEngine = null;
	private JavaScriptLoadWorkerStateListener javaScriptLoadWorkerStateListener = null;

	public JavaScriptListerner(WebEngine webEngine) {
		setWebEngine(webEngine);
		jsObject = (JSObject) getWebEngine().executeScript("window");// 获取js对象
	}

	/**
	 * 綁定含有可执行函数的对象
	 * 
	 * @param member
	 */
	public void binding(Object member) {
		System.out.println();
		((JavaScriptInterface) member).setWebEngine(webEngine);
		jsObject.setMember(member.getClass().getSimpleName(), member);// 然后把应用程序对象设置成为js对象
	}

	public void setWebEngine(WebEngine webEngine) {
		this.webEngine = webEngine;
		/**
		 * 增加alert弹窗
		 */
		this.webEngine.setOnAlert(new EventHandler<WebEvent<String>>() {

			@Override
			public void handle(WebEvent<String> event) {
				// TODO Auto-generated method stub
				switch (event.getEventType().getName()) {
				case "WEB_ALERT":
					Alert alert = new Alert(AlertType.INFORMATION, event
							.getData());
					if (R.image.icon_png != null) {
						List<Object> platformImages = new ArrayList<Object>();
						platformImages.add(new Image(R.image.icon_png));
						// System.out.println(alert.getDialogPane().impl_createPeer());
						// alert.getOwner()
						// .impl_getPeer()
						// .setIcons(platformImages);
					}
					alert.setTitle(R.value.stageTitle);
					alert.showAndWait();
					// Stage dialogStage = new Stage();
					// dialogStage.initModality(Modality.WINDOW_MODAL);
					// dialogStage.setScene(new Scene(VBoxBuilder.create().
					// children(new Text("Hi"), new Button("Ok.")).
					// alignment(Pos.CENTER).padding(new Insets(50)).build()));
					// dialogStage.show();
					break;

				default:
					break;
				}

			}
		});
		this.webEngine
				.setCreatePopupHandler(new Callback<PopupFeatures, WebEngine>() {

					@Override
					public WebEngine call(PopupFeatures arg0) {
						WebView newWebView = new WebView();
						WebEngine newWebEngine = newWebView.getEngine();
						return newWebEngine;
					}
				});
		this.webEngine.getLoadWorker().stateProperty()
				.addListener(new ChangeListener<State>() {

					@Override
					public void changed(
							ObservableValue<? extends State> observable,
							State oldValue, State newValue) {
						// TODO Auto-generated method stub
						switch (newValue) {
						case CANCELLED:
							getJavaScriptLoadWorkerStateListener()
									.onCancelledListener();
							break;
						case READY:
							getJavaScriptLoadWorkerStateListener()
									.onReadyListener();
							break;
						case FAILED:
							getJavaScriptLoadWorkerStateListener()
									.onFailedListener();
							break;
						case RUNNING:
							getJavaScriptLoadWorkerStateListener()
									.onRunningListener();
							break;
						case SCHEDULED:
							getJavaScriptLoadWorkerStateListener()
									.onScheduledListener();
							break;
						case SUCCEEDED:
							getJavaScriptLoadWorkerStateListener()
									.onSuccessedListener();
						default:
							break;
						}

					}
				});
	}

	public WebEngine getWebEngine() {
		return webEngine;
	}

	public JavaScriptLoadWorkerStateListener getJavaScriptLoadWorkerStateListener() {
		if (javaScriptLoadWorkerStateListener == null) {
			javaScriptLoadWorkerStateListener = new JavaScriptLoadWorkerStateListener() {

				@Override
				public void onSuccessedListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onScheduledListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onRunningListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onReadyListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onFailedListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void onCancelledListener() {
					// TODO Auto-generated method stub

				}
			};
		}
		return javaScriptLoadWorkerStateListener;
	}

	public void setJavaScriptLoadWorkerStateListener(
			JavaScriptLoadWorkerStateListener javaScriptLoadWorkerStateListener) {
		this.javaScriptLoadWorkerStateListener = javaScriptLoadWorkerStateListener;
	}

	/**
	 * javascript的执行接口
	 * 
	 * @author Lyon_Yan
	 *
	 */
	public static abstract class JavaScriptInterface {
		private WebEngine webEngine = null;

		public void setWebEngine(WebEngine webEngine) {
			this.webEngine = webEngine;
		}

		public WebEngine getWebEngine() {
			return webEngine;
		}

		public abstract void exit();
	}

}
